# Rails Command Line

  • rails new

    The first argument we'll pass to the rails new command is the application name.

    bin/rails new app_name
    
    1

    When creating a new Rails application, you have the option to specify what kind of database and what kind of source code management system your application is going to use. This will save you a few minutes, and certainly many keystrokes.

    Let's see what a --git option and a --database=postgresql option will do for us:

    rails new . --git --database=postgresql
    
    1
  • rails serve

    The bin/rails servercommand launches a web server named Puma which comes bundled with Rails. You'll use this any time you want to access your application through a web browser.

    bin/rails serve
    
    #provide port and environment
    bin/rails server -e production -p 4000
    
    
    1
    2
    3
    4
    5
  • rails generate

    The bin/rails generate command uses templates to create a whole lot of things. Running bin/rails generate by itself gives a list of available generators

    $ bin/rails generate
    Usage: rails generate GENERATOR [args] [options]
    
    ...
    ...
    
    Please choose a generator below.
    
    Rails:
      assets
      channel
      controller
      generator
      ...
      ...
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15

    contorller

    The controller generator is expect parameters in the form of generate controller ControllerName action1 action2. Let's make a Greetingscontroller with an action of hello , which will say something nice to us.

    $ bin/rails generate controller Greetings hello
         create  app/controllers/greetings_controller.rb
          route  get 'greetings/hello'
         invoke  erb
         create    app/views/greetings
         create    app/views/greetings/hello.html.erb
         invoke  test_unit
         create    test/controllers/greetings_controller_test.rb
         invoke  helper
         create    app/helpers/greetings_helper.rb
         invoke    test_unit
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11

    Model

    $ bin/rails generate model
    Usage:
      bin/rails generate model NAME [field[:type][:index] field[:type][:index]] [options]
    
    ...
    
    ActiveRecord options:
          [--migration], [--no-migration]        # Indicates when to generate migration
                                                 # Default: true
    
    ...
    
    Description:
        Generates a new model. Pass the model name, either CamelCased or
        under_scored, and an optional list of attribute pairs as arguments.
    
    ...
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17

    scaffold

    scaffold in Rails is a full set of model, database migration for that model, controller to manipulate it, views to view and manipulate the data, and a test suite for each of the above.

    $ bin/rails generate scaffold HighScore game:string score:integer
        invoke  active_record
        create    db/migrate/20190416145729_create_high_scores.rb
        create    app/models/high_score.rb
        invoke    test_unit
        create      test/models/high_score_test.rb
        create      test/fixtures/high_scores.yml
        invoke  resource_route
         route    resources :high_scores
        invoke  scaffold_controller
        create    app/controllers/high_scores_controller.rb
        invoke    erb
        create      app/views/high_scores
        create      app/views/high_scores/index.html.erb
        create      app/views/high_scores/edit.html.erb
        create      app/views/high_scores/show.html.erb
        create      app/views/high_scores/new.html.erb
        create      app/views/high_scores/_form.html.erb
        invoke    test_unit
        create      test/controllers/high_scores_controller_test.rb
        create      test/system/high_scores_test.rb
        invoke    helper
        create      app/helpers/high_scores_helper.rb
        invoke      test_unit
        invoke    jbuilder
        create      app/views/high_scores/index.json.jbuilder
        create      app/views/high_scores/show.json.jbuilder
        create      app/views/high_scores/_high_score.json.jbuilder
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
  • rails routes

    bin/rails routeswill list all of your defined routes, which is useful for tracking down routing problems in your app, or giving you a good overview of the URLs in an app you're trying to get familiar with.

  • rails db:create

  • rails db:migrate

    migrate the databa

  • rails console

    The console command lets you interact with your Rails application from the command line. On the underside, bin/rails console uses IRB, so if you've ever used it, you'll be right at home. This is useful for testing out quick ideas with code and changing data server-side without touching the website.

    You can specify the environment in which the console command should operate.

    bin/rails console -e staging
    
    1

    If you wish to test out some code without changing any data, you can do that by invoking bin/rails console --sandbox

    $ bin/rails console --sandbox
    Loading development environment in sandbox (Rails 7.0.0)
    Any modifications you make will be rolled back on exit
    irb(main):001:0>
    
    1
    2
    3
    4
  • rails dbconsole

    bin/rails dbconsolefigures out which database you're using and drops you into whichever command line interface you would use with it (and figures out the command line parameters to give to it, too!). It supports MySQL (including MariaDB), PostgreSQL, and SQLite3.

    bin/rails dbconsole --database=animals
    
    1
  • rails test

    Rails comes with a test framework called minitest. Rails owes its stability to the use of tests. The commands available in the test:  namespace helps in running the different tests you will hopefully write.

  • rails notes

    bin/rails notes searches through your code for comments beginning with a specific keyword. You can refer to bin/rails notes --help for information about usage.

    By default, it will search in appconfigdblib, and test directories for FIXME, OPTIMIZE, and TODO annotations in files with extension .builder.rb.rake.yml.yaml.ruby.css.js, and .erb.

    $ bin/rails notes
    app/controllers/admin/users_controller.rb:
      * [ 20] [TODO] any other way to do this?
      * [132] [FIXME] high priority for next deploy
    
    lib/school.rb:
      * [ 13] [OPTIMIZE] refactor this code to make it faster
      * [ 17] [FIXME]
    
    1
    2
    3
    4
    5
    6
    7
    8
  • rails assets

    You can precompile the assets in app/assets using bin/rails assets:precompile, and remove older compiled assets using bin/rails assets:clean. The assets:clean command allows for rolling deploys that may still be linking to an old asset while the new assets are being built.

    If you want to clear public/assets completely, you can use bin/rails assets:clobber.

  • rails about

    bin/rails aboutgives information about version numbers for Ruby, RubyGems, Rails, the Rails subcomponents, your application's folder, the current Rails environment name, your app's database adapter, and schema version. It is useful when you need to ask for help, check if a security patch might affect you, or when you need some stats for an existing Rails installation.

  • rails destroy

    Think of destroy as the opposite of generate. It'll figure out what generate did, and undo it.